﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZY.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZY.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:View_Tab_RoleInfo
    /// </summary>
    public partial class View_Tab_RoleInfo : IView_Tab_RoleInfo
    {
        public View_Tab_RoleInfo()
        { }
        #region  BasicMethod


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZY.Model.View_Tab_RoleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into View_Tab_RoleInfo(");
            strSql.Append("r_upid,rupName,r_leve,rleve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3)");
            strSql.Append(" values (");
            strSql.Append("@r_upid,@rupName,@r_leve,@rleve,@r_createtime,@r_lasttime,@r_note,@r_ext1,@r_ext2,@r_ext3)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@r_upid", SqlDbType.Int,4),
					new SqlParameter("@rupName", SqlDbType.VarChar,50),
					new SqlParameter("@r_leve", SqlDbType.Int,4),
					new SqlParameter("@rleve", SqlDbType.VarChar,8),
					new SqlParameter("@r_createtime", SqlDbType.DateTime),
					new SqlParameter("@r_lasttime", SqlDbType.DateTime),
					new SqlParameter("@r_note", SqlDbType.VarChar,5000),
					new SqlParameter("@r_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext3", SqlDbType.VarChar,50)};
            parameters[0].Value = model.r_upid;
            parameters[1].Value = model.rupName;
            parameters[2].Value = model.r_leve;
            parameters[3].Value = model.rleve;
            parameters[4].Value = model.r_createtime;
            parameters[5].Value = model.r_lasttime;
            parameters[6].Value = model.r_note;
            parameters[7].Value = model.r_ext1;
            parameters[8].Value = model.r_ext2;
            parameters[9].Value = model.r_ext3;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZY.Model.View_Tab_RoleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update View_Tab_RoleInfo set ");
            strSql.Append("r_upid=@r_upid,");
            strSql.Append("rupName=@rupName,");
            strSql.Append("r_leve=@r_leve,");
            strSql.Append("rleve=@rleve,");
            strSql.Append("r_createtime=@r_createtime,");
            strSql.Append("r_lasttime=@r_lasttime,");
            strSql.Append("r_note=@r_note,");
            strSql.Append("r_ext1=@r_ext1,");
            strSql.Append("r_ext2=@r_ext2,");
            strSql.Append("r_ext3=@r_ext3");
            strSql.Append(" where rid=@rid");
            SqlParameter[] parameters = {
					new SqlParameter("@r_upid", SqlDbType.Int,4),
					new SqlParameter("@rupName", SqlDbType.VarChar,50),
					new SqlParameter("@r_leve", SqlDbType.Int,4),
					new SqlParameter("@rleve", SqlDbType.VarChar,8),
					new SqlParameter("@r_createtime", SqlDbType.DateTime),
					new SqlParameter("@r_lasttime", SqlDbType.DateTime),
					new SqlParameter("@r_note", SqlDbType.VarChar,5000),
					new SqlParameter("@r_ext1", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext2", SqlDbType.VarChar,50),
					new SqlParameter("@r_ext3", SqlDbType.VarChar,50),
					new SqlParameter("@rid", SqlDbType.Int,4)};
            parameters[0].Value = model.r_upid;
            parameters[1].Value = model.rupName;
            parameters[2].Value = model.r_leve;
            parameters[3].Value = model.rleve;
            parameters[4].Value = model.r_createtime;
            parameters[5].Value = model.r_lasttime;
            parameters[6].Value = model.r_note;
            parameters[7].Value = model.r_ext1;
            parameters[8].Value = model.r_ext2;
            parameters[9].Value = model.r_ext3;
            parameters[10].Value = model.rid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int rid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_RoleInfo ");
            strSql.Append(" where rid=@rid");
            SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)
			};
            parameters[0].Value = rid;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string ridlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from View_Tab_RoleInfo ");
            strSql.Append(" where rid in (" + ridlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_RoleInfo GetModel(int rid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 rid,r_upid,rupName,r_leve,rleve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 from View_Tab_RoleInfo ");
            strSql.Append(" where rid=@rid");
            SqlParameter[] parameters = {
					new SqlParameter("@rid", SqlDbType.Int,4)
			};
            parameters[0].Value = rid;

            ZY.Model.View_Tab_RoleInfo model = new ZY.Model.View_Tab_RoleInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZY.Model.View_Tab_RoleInfo DataRowToModel(DataRow row)
        {
            ZY.Model.View_Tab_RoleInfo model = new ZY.Model.View_Tab_RoleInfo();
            if (row != null)
            {
                if (row["rid"] != null && row["rid"].ToString() != "")
                {
                    model.rid = int.Parse(row["rid"].ToString());
                }
                if (row["r_upid"] != null && row["r_upid"].ToString() != "")
                {
                    model.r_upid = int.Parse(row["r_upid"].ToString());
                }
                if (row["rupName"] != null)
                {
                    model.rupName = row["rupName"].ToString();
                }
                if (row["r_leve"] != null && row["r_leve"].ToString() != "")
                {
                    model.r_leve = int.Parse(row["r_leve"].ToString());
                }
                if (row["rleve"] != null)
                {
                    model.rleve = row["rleve"].ToString();
                }
                if (row["r_createtime"] != null && row["r_createtime"].ToString() != "")
                {
                    model.r_createtime = DateTime.Parse(row["r_createtime"].ToString());
                }
                if (row["r_lasttime"] != null && row["r_lasttime"].ToString() != "")
                {
                    model.r_lasttime = DateTime.Parse(row["r_lasttime"].ToString());
                }
                if (row["r_note"] != null)
                {
                    model.r_note = row["r_note"].ToString();
                }
                if (row["r_ext1"] != null)
                {
                    model.r_ext1 = row["r_ext1"].ToString();
                }
                if (row["r_ext2"] != null)
                {
                    model.r_ext2 = row["r_ext2"].ToString();
                }
                if (row["r_ext3"] != null)
                {
                    model.r_ext3 = row["r_ext3"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select rid,r_upid,rupName,r_leve,rleve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 ");
            strSql.Append(" FROM View_Tab_RoleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" rid,r_upid,rupName,r_leve,rleve,r_createtime,r_lasttime,r_note,r_ext1,r_ext2,r_ext3 ");
            strSql.Append(" FROM View_Tab_RoleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM View_Tab_RoleInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.rid desc");
            }
            strSql.Append(")AS Row, T.*  from View_Tab_RoleInfo T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "View_Tab_RoleInfo";
            parameters[1].Value = "rid";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

